Crate json_stream

source ·
Expand description

A library to parse Newline Delimited JSON values from a byte stream.

Example

use json_stream::JsonStream;
use futures::stream::once;
use futures::StreamExt;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Foo {
   bar: String,
}

#[tokio::main]
async fn main() {
    // This could be any stream that yields bytes, such as a file stream or a network stream.
    let pinned_bytes_future = Box::pin(async {
        Ok::<_, std::io::Error>(r#"{"bar": "foo"}\n{"bar": "qux"}\n{"bar": "baz"}"#.as_bytes())
    });
    let mut json_stream = JsonStream::<Foo, _>::new(once(pinned_bytes_future));

    while let Some(Ok(foo)) = json_stream.next().await {
        println!("{:?}", foo);
    }
}

Structs

  • A [Stream] implementation that can be used to parse Newline Delimited JSON values from a byte stream. It does so by buffering bytes internally and parsing them as they are received. This means that the stream will not yield values until a full JSON value has been received.

Constants